/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package service.reportCenter.rawGoodsInventoryReport;

import com.openbrew.openbrewrf.Hops;
import com.openbrew.openbrewrf.Malt;
import com.openbrew.openbrewrf.Supplies;
import com.openbrew.openbrewrf.Wearhouse;
import com.openbrew.openbrewrf.views.HopInventoryView;
import com.openbrew.openbrewrf.views.MaltInventoryView;
import com.openbrew.openbrewrf.views.SupplyInventoryView;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import tools.InventoryModifiers;

/**
 *
 * @author dchapman
 */
@Stateless
@Path("com.openbrew.openbrewrf.rawgoodsinventoryreport")
public class RawGoodsInventoryReportREST {

    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public RawGoodsInventoryReportREST() {
    }
    
    @GET
    @Path("getRawGoodsInventoryReport")
    @Produces(MediaType.APPLICATION_XML)
    public RawGoodsInvReport runFullInventoryReport() {
        final InventoryModifiers im = new InventoryModifiers();
        im.runSupplyAllocationFix(em);
        im.runMaltAllocationFix(em);
        im.runHopAllocationFix(em);
        
        RawGoodsInvReport inventory = new RawGoodsInvReport();
        inventory.setMaltCollection(getMalts(em));
        inventory.setHopColletion(getHops(em));
        List<Wearhouse> whs = getWarehouses(em);
        List<WarehouseInventory> whis = new ArrayList<WarehouseInventory>();
        for(final Wearhouse wh : whs){
            List<SupplyInventoryView> supplies = getSuppliesForWarehouse(em, wh);
            if(!supplies.isEmpty()){
                WarehouseInventory wi = new WarehouseInventory();
                wi.setSupplyCollection(supplies);
                wi.setWarehouseId(wh.getWearhouseID());
                wi.setWarehouseName(wh.getWHDesc());
                whis.add(wi);
            }
        }
        inventory.setWhCollection(whis);
        return inventory;
    }
    
    private List<MaltInventoryView> getMalts(final EntityManager en){
        TypedQuery<MaltInventoryView> query = en.createNamedQuery("MaltInventoryView.findAll", MaltInventoryView.class);
        return query.getResultList();
    }
    
    private List<HopInventoryView> getHops(final EntityManager en){
        TypedQuery<HopInventoryView> query = en.createNamedQuery("HopInventoryView.findAll", HopInventoryView.class);
        return query.getResultList();
    }
    
    private List<SupplyInventoryView> getSuppliesForWarehouse(final EntityManager en, final Wearhouse wh){
        TypedQuery<SupplyInventoryView> query = en.createNamedQuery("SupplyInventoryView.findBySupWH", SupplyInventoryView.class);
        query.setParameter(1, wh.getWearhouseID());
        return query.getResultList();
    }
    
    private List<Wearhouse> getWarehouses(final EntityManager en){
        TypedQuery<Wearhouse> query = en.createNamedQuery("Wearhouse.findAll", Wearhouse.class);
        return query.getResultList();
    }

    @PUT
    @Consumes({"application/xml", "application/json"})
    public void edit(RawGoodsInvReport invReport) {
        for(final HopInventoryView hop : invReport.getHopColletion()){
            getEntityManager().merge(hop);
        }
        for(final MaltInventoryView malt : invReport.getMaltCollection()){
            getEntityManager().merge(malt);
        }
        for(final WarehouseInventory wh : invReport.getWhCollection()){
            for(final SupplyInventoryView supply : wh.getSupplyCollection()){
                getEntityManager().merge(supply);
            }
        }
        this.getEntityManager().getEntityManagerFactory().getCache().evict(Supplies.class);
        this.getEntityManager().getEntityManagerFactory().getCache().evict(Malt.class);
        this.getEntityManager().getEntityManagerFactory().getCache().evict(Hops.class);
        this.getEntityManager().getEntityManagerFactory().getCache().evictAll();
    }

    protected EntityManager getEntityManager() {
        return em;
    }
    
}
